﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebShop.Models;

namespace WebShop.Controllers
{
     [Authorize]
    public class ShoppingController : Controller
    {
        private WGRUSEntities db = new WGRUSEntities();

        //
        // GET: /Shopping/

        public ActionResult Index()
        {
            UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName == User.Identity.Name);
            ViewData["ItemCount"] = db.Baskets.Count(b => b.Customer_Id == user.Customer_Id).ToString();

            List<ShoppingModel> model = (from i in db.Items
                where i.Active == true
                select new ShoppingModel
                {
                    Quantity = 1,
                    Code = i.Code,
                    Description = i.Description,
                    UnitPrice = i.UnitPrice
                }).ToList();
            return View("Shopping",model);
        }

       
        [ActionName("AddItem")]
        public string AddItem(ShoppingModel model)
        {
            UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName == User.Identity.Name);

            Basket item = new Basket
            {
                Item_Code = model.Code,
                Item_Description = model.Description,
                Quantity = model.Quantity,
                UnitPrice = model.UnitPrice,
                Customer_Id = user.Customer_Id
            };

            db.Baskets.Add(item);
            db.SaveChanges();

            return db.Baskets.Count(b => b.Customer_Id == user.Customer_Id).ToString();

        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}